package com.jk.smartelderlycare.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jk.smartelderlycare.dao.SysLabelMapper;
import com.jk.smartelderlycare.dao.SysTutionMapper;
import com.jk.smartelderlycare.dto.SysLabel;
import com.jk.smartelderlycare.dto.SysTution;
import com.jk.smartelderlycare.pojo.PagedResponse;
import com.jk.smartelderlycare.service.SysTutionService;
import com.jk.smartelderlycare.utils.PaginationUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * @author hhk
 * @version 1.0
 * @ClassName SysTutionServiceImpl
 * @program: smart-elderly-care
 * @description: TODO
 * @date 2024/11/13 18:59
 */
@Service
public class SysTutionServiceImpl implements SysTutionService {

    @Autowired
    private SysTutionMapper sysTutionMapper;

    @Autowired
    private SysLabelMapper sysLabelMapper;
    /**
     * @description: 机构列表后端分页查询
     * @param: [sysTution, pageNum, pageSize]
     * @return: com.jk.smartelderlycare.pojo.PagedResponse<com.jk.smartelderlycare.dto.SysTution>
     * @author hhk
     * @date: 2024/11/13 19:01
     */
    @Override
    public PagedResponse<SysTution> findSysTutionPagimationList(SysTution sysTution, Integer pageNum, Integer pageSize) {
        Page<SysTution> page = new Page<>(pageNum,pageSize);
        IPage<SysTution> sysTutionList= sysTutionMapper.findSysTutionPagimationList(sysTution,page);
        return PaginationUtil.getPagedResponse(sysTutionList.getRecords(),pageNum,pageSize,sysTutionList.getTotal());
    }
    /**
     * @description: 机构列表新增
     * @param: [sysTution]
     * @return: void
     * @author hhk
     * @date: 2024/11/13 19:46
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void addSysTution(SysTution sysTution) {
        sysTutionMapper.insert(sysTution);
        //新增中间表
        HashMap<String, Object> map = new HashMap<>();
        if(null!=sysTution.getLabelIds()){
            //循环labelId
            for (Long labelId : sysTution.getLabelIds()) {
                map.put("tutionId",sysTution.getTutionId());
                map.put("labelId",labelId);
                sysTutionMapper.addSysTutionAndLabel(map);
            }
        }
    }
    /**
     * @description: 根据id删除机构列表
     * @param: [tutionId]
     * @return: void
     * @author hhk
     * @date: 2024/11/13 19:46
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteSysTutionByTutionId(Long tutionId) {
        sysTutionMapper.deleteById(tutionId);
        //删除中间表
        sysTutionMapper.deleteSysTutionAndLabelByTutionId(tutionId);
    }
    /**
     * @description: 根据id回显列表信息
     * @param: [tutionId]
     * @return: com.jk.smartelderlycare.dto.SysTution
     * @author hhk
     * @date: 2024/11/13 19:46
     */

    @Override
    public SysTution findSysTutionByTutionId(Long tutionId) {
        //根据id查询用户信息
       SysTution sysTution = sysTutionMapper.findSysTutionByTutionId(tutionId);
        if(null != sysTution.getStrLabel() && !"".equals(sysTution.getStrLabel())){
            String[] labelArr = sysTution.getStrLabel().split(",");
            ArrayList<Long> labelList = new ArrayList<>();
            for (String labelId : labelArr) {
                labelList.add(Long.parseLong(labelId));
            }
            sysTution.setLabelIds(labelList);
        }
        return sysTution;
    }

    /**
     * @description: 修改列表信息
     * @param: [sysTution]
     * @return: void
     * @author hhk
     * @date: 2024/11/13 19:46
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updataSysTution(SysTution sysTution) {
        sysTutionMapper.updateById(sysTution);
        //二、修改中间表：先删后增
        //1.根据tutionId删除中间表
        sysTutionMapper.deleteSysTutionAndLabelByTutionId(sysTution.getTutionId());
        //新增
        HashMap<String, Object> map = new HashMap<>();
        if(null!=sysTution.getLabelIds()){
            //循环labelId
            for (Long labelId : sysTution.getLabelIds()) {
                map.put("tutionId",sysTution.getTutionId());
                map.put("labelId",labelId);
                sysTutionMapper.addSysTutionAndLabel(map);
            }
        }
    }
    /**
     * @description: 标签动态下拉列表
     * @param: []
     * @return: java.util.List<com.jk.smartelderlycare.dto.SysLabel>
     * @author hhk
     * @date: 2024/11/13 19:46
     */
    @Override
    public List<SysLabel> findSysLabelList() {
        return sysLabelMapper.selectList(null);
    }
}
